package com.hezi.umeng;

import android.app.Application;
import android.app.Notification;
import android.content.Context;

import com.hezi.umeng.utils.LogUtils;
import com.hezi.umeng.utils.ToastUtils;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

/**
 * Created by Kvin on 2017/1/2.
 */
public class BaseApp extends Application {
    private static BaseApp app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initUmeng();
    }

    /**
     * return app instance
     */
    public static BaseApp getInstance() {
        return app;
    }

    /**
     * init umeng api
     */
    private void initUmeng() {
        //register platform
        UMShareAPI.get(this);
        PlatformConfig.setWeixin("wx967daebe835fbeac", "5bb696d9ccd75a38c8a0bfe0675559b3");
        //PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad");
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");

        //init push
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.setDebugMode(true);
        mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
        mPushAgent.register(mUmengRegisterCallback);
        mPushAgent.setMessageHandler(mUmengMessageHandler);
        mPushAgent.setNotificationClickHandler(mUmengNotificationClickHandler);
        LogUtils.e("------registerId=" + mPushAgent.getRegistrationId());
    }

    /**
     * umeng register callback
     */
    private IUmengRegisterCallback mUmengRegisterCallback = new IUmengRegisterCallback() {
        @Override
        public void onSuccess(String s) {
            LogUtils.v("me-------------------------------", s);
            // ToastUtils.simpleToast(getInstance(), s);
        }

        @Override
        public void onFailure(String s, String s1) {
            //ToastUtils.simpleToast(getInstance(), s + ":" + s1);
        }
    };

    /**
     * umeng message handler
     */
    private UmengMessageHandler mUmengMessageHandler = new UmengMessageHandler() {
        @Override
        public void dealWithCustomMessage(Context context, UMessage uMessage) {
            super.dealWithCustomMessage(context, uMessage);
            LogUtils.e("UmengMessageHandler", "dealWithCustomMessage---" + uMessage);
        }

        @Override
        public Notification getNotification(Context context, UMessage uMessage) {
            LogUtils.e("UmengMessageHandler", "getNotification----" + uMessage.msg_id + ":" + uMessage.title + ":" + uMessage.text + ":" + uMessage.url);
            return super.getNotification(context, uMessage);
        }
    };

    /**
     * umeng push notificaton click event
     */
    private UmengNotificationClickHandler mUmengNotificationClickHandler = new UmengNotificationClickHandler() {
        @Override
        public void dealWithCustomAction(Context context, UMessage uMessage) {
            super.dealWithCustomAction(context, uMessage);
//            ToastUtils.simpleToast(getInstance(), uMessage.toString());
            LogUtils.e("------" + uMessage.toString());
        }

        @Override
        public void handleMessage(Context context, UMessage uMessage) {
            super.handleMessage(context, uMessage);
            ToastUtils.simpleToast(getInstance(), uMessage.msg_id + ":" + uMessage.title + ":" + uMessage.text + ":" + uMessage.url);
        }
    };

}
